package com.aatattoo.light_food_service.utils;
/**
 * 功能描述
 *List排序工具类针对Int值
 * @author 27180
 * @date 2022/07/01  11:23
 */

import org.thymeleaf.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author 27180
 * @date 2022/7/1 11:23
 */
public class SortListUtil<E> {
    public void sort(List<E> list,final String method ,String order){
        Collections.sort(list,new Comparator<E>() {
            @Override
            public int compare(E o1, E o2) {
                int ret = 0;//作为返回值   返回的结果是比较的结果
                try {
                Method method1  = o1.getClass().getMethod(method,null);//取出的东西是o1里传入方法名字的返回值
                Method method2  = o2.getClass().getMethod(method,null);//么个取出的是o2传入方法的返回值
                    Object invoke = method2.invoke(o1, null);
                    //倒叙    排列
                    if (!StringUtils.isEmpty(order) && "desc".equalsIgnoreCase(order)){
                        //ret = method2.invoke(o2,null).toString().compareTo(method1.invoke(o1,null).toString());
                        Object o = method1.invoke(o1, null);
                        int a = Integer.parseInt(o.toString());
                        o = method2.invoke(o2,null);
                        int b = Integer.parseInt(o.toString());
                        ret = b - a;
                    }else {
                        //ret = method1.invoke(o1,null).toString().compareTo(method2.invoke(o2,null).toString());
                        Object o = method1.invoke(o1, null);
                        int a = Integer.parseInt(o.toString());
                        o = method2.invoke(o2,null);
                        int b = Integer.parseInt(o.toString());
                        System.out.println(a+"a++++++++b"+b);//这个用来看ab分别的值
                        ret = a-b;
                        System.out.println(ret+"这是返回的值");
                        System.out.println(list+"这是每一步交换的列表");
                    }
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                return ret;
            }
        });
    }
}